<template>
    <div>
        <el-dialog title="新增" v-model="isShowDialog" width="600px" @close="closeDialog">
            <el-form ref="formRef" :model="formModel" :rules="formRules" size="default" label-width="120px">
                <el-form-item label="组名称" prop="groupName">
                    <el-input v-model="formModel.groupName" show-word-limit maxlength="30" placeholder=""
                        clearable></el-input>
                </el-form-item>
                <el-form-item label="执行类型" prop="executeType">
                    <el-select v-model="formModel.executeType" placeholder="">
                        <el-option v-for="item in DictInfo.groupExecuteType" :key="item.key" :label="item.label"
                            :value="item.key" />
                    </el-select>
                </el-form-item>
                <el-form-item v-if="formModel.executeType === 2" label="间隔时间(S)" prop="delaySeconds"
                    :rules="[{ required: true, message: '间隔时间不能为空', trigger: 'change' }]">
                    <el-input-number v-model="formModel.delaySeconds" show-word-limit :min="1" :max="999999" placeholder=""
                        clearable></el-input-number>
                </el-form-item>
                <el-form-item v-if="formModel.executeType === 1" label="cron表达式" prop="cron"
                    :rules="[{ required: true, message: 'cron表达式不能为空', trigger: 'change' }]">
                    <el-input v-model="formModel.cron" show-word-limit maxlength="50" placeholder="" clearable></el-input>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formModel.remark" show-word-limit maxlength="400" placeholder=""
                        clearable></el-input>
                </el-form-item>
            </el-form>
            <el-divider content-position="left">任务顺序</el-divider>
            <TaskRelation @onload="onCheckTask"></TaskRelation>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="() => closeDialog()" size="default">取 消</el-button>
                    <el-button type="primary" :loading="submitLoading" @click="onSubmit" size="default">提 交</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, defineComponent, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { TaskGroupInfoAddParamModel } from '/@/interfaces/DataPipelineModel';
import { handleAdd, handleValidateForm } from '../task-group-info-wrap';
import TaskRelation from './task-relation.vue';
import { DictInfo } from '/@/enums/dict-info-enum';

interface State {
    isShowDialog: boolean | true,
    submitLoading: boolean | false,
    formModel: TaskGroupInfoAddParamModel,
    formRules: { [key: string]: any[] },
}

export default defineComponent({
    name: 'TaskGroupInfoAdd',
    components: { TaskRelation },
    emits: ['onClose'],
    setup(props, context) {
        const state = reactive<State>({
            isShowDialog: true,
            submitLoading: false,
            formModel: {
                groupName: undefined,
                executeType: undefined,
                delaySeconds: undefined,
                cron: undefined,
                remark: undefined,
                taskIdList: [],
            },
            formRules: {
                groupName: [{ required: true, message: '组名称不能为空', trigger: 'change' }],
                executeType: [{ required: true, message: '执行类型不能为空', trigger: 'change' }]
            },
        });
        const methodMap = reactive({
            // 关闭弹窗
            closeDialog: function () {
                state.isShowDialog = false;
                context.emit('onClose');
            },
            // 新增
            onSubmit: async function () {
                state.submitLoading = true;
                setTimeout(() => state.submitLoading = false, 1500);
                if (state.formModel.executeType === 1) {
                    state.formModel.delaySeconds = undefined;
                } else {
                    state.formModel.cron = undefined;
                }
                // 表单验证
                let validate = await handleValidateForm(formRef.value);
                if (!validate) {
                    state.submitLoading = false;
                    return;
                }
                // 提交
                await handleAdd({ param: state.formModel, pageInfo: undefined }, methodMap.closeDialog);
            },
            onCheckTask: function (ids: string[]) {
                state.formModel.taskIdList = [...ids];
            },
        });
        const formRef = ref<FormInstance>();
        const executeStatusRef = ref();
        return {
            DictInfo,
            executeStatusRef,
            formRef,
            ...toRefs(state),
            ...toRefs(methodMap),
        };
    },
});
</script>
